AS = nasm
CC = /usr/local/i386elfgcc/bin/i386-elf-gcc
LD = /usr/local/i386elfgcc/bin/i386-elf-ld
RM = rm -f
DD = dd
OBJCOPY = /usr/local/i386elfgcc/bin/i386-elf-objcopy
EMU = qemu

CFLAGS = -Wall -std=c99 -m32 -nostdlib -nostdinc -ffreestanding -fno-stack-protector -I./includes 
LDFLAGS = -T link.ld --warn-common
EMUFLAGS = -full-screen -net none -fda


all: clean floppy.img

run: clean floppy.img
	$(EMU) $(EMUFLAGS) floppy.img

clean:
	$(RM) *.img
	$(RM) boot/*.bin
	$(RM) boot/*.o
	$(RM) kernel/*.bin
	$(RM) kernel/*.o

floppy.img: bootsect.bin kernel.bin
	$(DD) if=/dev/zero of=floppy.img bs=512 count=2880
	$(DD) if=boot/bootsect.bin of=floppy.img conv=notrunc
	$(DD) if=kernel/kernel.bin of=floppy.img conv=notrunc seek=1

bootsect.bin:
	$(AS) boot/bootsect.asm -f bin -o boot/bootsect.bin

kernel.bin:
	$(AS) boot/init.asm -f elf -o boot/init_asm.o
	$(CC) $(CFLAGS) -c boot/init.c -o boot/init.o
	$(CC) $(CFLAGS) -c kernel/mem.c -o kernel/mem.o
	$(CC) $(CFLAGS) -c kernel/string.c -o kernel/string.o
	$(CC) $(CFLAGS) -c kernel/video.c -o kernel/video.o
	$(CC) $(CFLAGS) -c kernel/io.c -o kernel/io.o
	$(LD) $(LDFLAGS) -o kernel/kernel.o boot/init_asm.o boot/init.o kernel/mem.o kernel/string.o kernel/video.o kernel/io.o
	$(OBJCOPY) -v -O binary kernel/kernel.o kernel/kernel.bin
	

